package cn.iocoder.yudao.module.signature.controller.admin.developeraccount;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccount.vo.DeveloperAccountPageReqVO;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccount.vo.DeveloperAccountRespVO;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccount.vo.DeveloperAccountSaveReqVO;
import cn.iocoder.yudao.module.signature.enums.AppleAccountType;
import cn.iocoder.yudao.module.signature.service.developeraccount.DeveloperAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 苹果开发者账号")
@RestController
@RequestMapping("/signature/developer-account")
@Validated
public class DeveloperAccountController {

    @Resource
    private DeveloperAccountService developerAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建苹果开发者账号")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:create')")
    public CommonResult<Long> createDeveloperAccount(@Valid @RequestBody DeveloperAccountSaveReqVO createReqVO) throws Exception {
        createReqVO.setAccountBelongType(String.valueOf(AppleAccountType.PUBLIC_POOL.getType()));
        return success(developerAccountService.createDeveloperAccount(createReqVO));
    }

    @PostMapping("/refreshRemaining")
    @Operation(summary = "刷新余量")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:refreshRemaining')")
    public CommonResult<Boolean> createDeveloperAccount(@RequestParam("id") Long id) throws Exception {
        developerAccountService.refreshRemaining(id, null);
        return success(true);
    }

    @PostMapping("/downloadMobileProvision")
    @Operation(summary = "下载描述文件")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:downloadMobileProvision')")
    public CommonResult<Boolean> downloadMobileProvision(@RequestParam("id") Long id) throws Exception {
        developerAccountService.downloadMobileProvision(id, null);
        return success(true);
    }


    @PostMapping("/refreshProfile")
    @Operation(summary = "刷新权限")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:refreshProfile')")
    public CommonResult<Boolean> refreshProfile(@RequestParam("id") Long id) throws Exception {
        developerAccountService.refreshProfile(id);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新苹果开发者账号")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:update')")
    public CommonResult<Boolean> updateDeveloperAccount(@Valid @RequestBody DeveloperAccountSaveReqVO updateReqVO) {
        developerAccountService.updateDeveloperAccount(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除苹果开发者账号")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('signature:developer-account:delete')")
//    public CommonResult<Boolean> deleteDeveloperAccount(@RequestParam("id") Long id) {
//        developerAccountService.deleteDeveloperAccount(id,null);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得苹果开发者账号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:query')")
    public CommonResult<DeveloperAccountRespVO> getDeveloperAccount(@RequestParam("id") Long id) {
        return success(developerAccountService.getDeveloperAccount(id, null));
    }

    @GetMapping("/page")
    @Operation(summary = "获得苹果开发者账号分页")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:query')")
    public CommonResult<PageResult<DeveloperAccountRespVO>> getDeveloperAccountPage(@Valid DeveloperAccountPageReqVO pageReqVO) {
        return success(developerAccountService.getDeveloperAccountPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出苹果开发者账号 Excel")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeveloperAccountExcel(@Valid DeveloperAccountPageReqVO pageReqVO,
                                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeveloperAccountRespVO> list = developerAccountService.getDeveloperAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "苹果开发者账号.xls", "数据", DeveloperAccountRespVO.class,
                list);
    }


    @PostMapping("/delete")
    @Operation(summary = "删除数据（单个或者批量）")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:delete')")
    public CommonResult<Boolean> delete(@RequestBody List<Long> ids) {
        developerAccountService.delete(ids);
        return success(Boolean.TRUE);
    }

    @PostMapping("/enable")
    @Operation(summary = "启用（单个或者批量）")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:enable')")
    public CommonResult<Boolean> enable(@RequestBody List<Long> ids) {
        developerAccountService.enable(ids);
        return success(Boolean.TRUE);
    }

    @PostMapping("/disable")
    @Operation(summary = "禁用（单个或者批量）")
    @PreAuthorize("@ss.hasPermission('signature:developer-account:disable')")
    public CommonResult<Boolean> disable(@RequestBody List<Long> ids) {
        developerAccountService.disable(ids);
        return success(Boolean.TRUE);
    }

}